package com.wceManage.manage.controller;

import com.wceManage.beans.SysRole;
import com.wceManage.beans.page.PageList;
import com.wceManage.commons.util.StringUtil;
import com.wceManage.manage.context.GlobalContext;
import com.wceManage.manage.util.UserUtils;
import com.wceManage.services.SysRoleService;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("sysrole")
public class SysRoleController extends BaseController {

    @Autowired
    private SysRoleService sysRoleService;

    @ModelAttribute("sysRole")
    public SysRole get(@RequestParam(required = false) String id) {
        if (StringUtil.isNotBlank(id)) {
            long _id = NumberUtils.toLong(id);
            return sysRoleService.queryById(_id);
        } else {
            return new SysRole();
        }
    }

    /**
     * 查询列表
     *
     * @param sysRole
     * @return
     */
    @RequiresPermissions("sys:role:view")
    @RequestMapping(value = {"", "/", "list"})
    public String list(SysRole sysRole, HttpServletRequest request) {
        request.setAttribute("sysRole", sysRole);
        return "sys/sysrole/list";
    }

    /**
     * 异步获取list资源
     *
     * @param pageList
     * @return
     */
    @RequiresPermissions("sys:role:view")
    @RequestMapping("asynList")
    @ResponseBody
    public PageList asynList(PageList pageList) {
        List<SysRole> roleList = UserUtils.getRoleList();
        pageList.setAaData(roleList);
        pageList.setiTotalRecords(roleList.size());
        pageList.setiTotalDisplayRecords(roleList.size());
        return pageList;
    }

    /**
     * 新增修改
     *
     * @param id
     * @return
     */
    @RequiresPermissions("sys:role:edit")
    @RequestMapping(value = {"add", "edit"}, method = RequestMethod.GET)
    public String edit(Long id, Model model) {
        SysRole sysRole;
        if (id == null || id < 1l) { //add
            sysRole = new SysRole();
        } else {//edit
            sysRole = sysRoleService.queryById(id);
        }
        model.addAttribute("sysRole", sysRole);
        model.addAttribute("menuList", UserUtils.getMenuList());

        return "sys/sysrole/edit";
    }

    @RequiresPermissions("sys:role:edit")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public String save(SysRole sysRole, HttpServletRequest request) {
        sysRole.setCreateBy(UserUtils.getUser().getUserId());
        sysRole.setUpdateBy(UserUtils.getUser().getUserId());
        sysRole.setSysData("0");

        if (sysRole.getRoleId() == null) { //add
            sysRoleService.add(sysRole);
        } else {
            sysRoleService.update(sysRole);
        }

        // 更新角色与菜单关联
        sysRoleService.deleteRoleResource(sysRole);
        if (sysRole.getResourceList().size() > 0){
            sysRoleService.insertRoleResource(sysRole);
        }

        // 清除用户角色缓存
        UserUtils.removeCache(UserUtils.CACHE_ROLE_LIST);
        //TODO  成功失败结果页面
        return "redirect:list";
    }

    @RequiresPermissions("sys:role:delete")
    @RequestMapping("delete")
    @ResponseBody
    public Object delete(SysRole sysRole) {
        String result = "";
        if(!UserUtils.getUser().isAdmin() && sysRole.getSysData().equals(GlobalContext.YES)){
          result = "越权操作，只有超级管理员才能修改此数据！";
            return result;
        }

        sysRole.setUpdateBy(UserUtils.getUser().getUserId());
        sysRoleService.delete(sysRole);
        result = "禁用角色 " + sysRole.getName() + "成功";
        UserUtils.removeCache(UserUtils.CACHE_ROLE_LIST);
        //TODO  成功失败结果页面
        return result;
    }

    /**
     * 验证角色名是否有效
     *
     * @param oldName
     * @param roleName
     * @return
     */
    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "checkName", method = RequestMethod.POST)
    public Boolean checkName(String oldName, String roleName) {
        if (roleName != null && roleName.equals(oldName)) {
            return true;
        } else if (roleName != null && sysRoleService.getRoleByName(roleName) == null) {
            return true;
        }
        return false;
    }
}
